package com.ebelter.myehc.ui.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.models.bluetooth.products.bracelet.bean.RateBean;
import com.ebelter.btcomlib.models.bluetooth.products.bracelet.bean.SleepInfo;
import com.ebelter.btcomlib.models.bluetooth.products.bracelet.bean.WalkBean;
import com.ebelter.btcomlib.utils.SpecialText;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.myehc.R;
import com.ebelter.myehc.adapters.BraFragmentAdapter;
import com.ebelter.myehc.beans.BraTodayData;
import com.ebelter.myehc.ui.activitys.MainActivity;
import com.ebelter.myehc.ui.fragments.bracelet.BRA_BS_Fragment;
import com.ebelter.myehc.ui.fragments.bracelet.BRA_SHM_Fragment;
import com.ebelter.myehc.ui.fragments.bracelet.BRA_XL_Fragment;
import java.util.ArrayList;
import java.util.List;

import static com.ebelter.btcomlib.bases.activity.BaseActivity.getString_;

/**
 * 手环
 */

public class BRA_Fragment extends BaseFragment {
    public static final String TAG = "BRA_Fragment";



    private ViewPager mViewPager;

    private BraFragmentAdapter mBraFragmentAdapter;
    private List<Fragment> listFragment;


    private RelativeLayout rl_dots;//底部滑动小圆点
    private LinearLayout in_ll;
    private ImageView mLight_dots;

    private TextView time_tv;

    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_bra;
    }

    private TextView activityTopTittleTv;

    public void setActivityTopTittleTv(TextView activityTopTittleTv) {
        this.activityTopTittleTv = activityTopTittleTv;
    }
    @Override
    protected void initView() {
        super.initView();
        mViewPager=mRootView.findViewById(R.id.bra_f_vp);

        rl_dots = mRootView.findViewById(R.id.rl_dots);
        in_ll = mRootView.findViewById(R.id.in_ll);
        mLight_dots = mRootView.findViewById(R.id.iv_light_dots);

        time_tv = mRootView.findViewById(R.id.bra_time_tv);



        addDots();
        getDistance();
    }


    private int pageSelectedPosition;
    public int getPageSelectedPosition(){
        return pageSelectedPosition;
    }


    @Override
    protected void setListener() {
        super.setListener();
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                setLightPoint(position,positionOffset);
            }

            @Override
            public void onPageSelected(int position) {
                pageSelectedPosition=position;

                braPageSelectedChange(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    //viewPager 滑动监听到
    private void braPageSelectedChange(int position) {
        if (getActivity()!=null) {
            ((MainActivity)getActivity()).braPageSelectedChange(position);
        }


    }

    private float mDistance = 20;
    private int mLeftRightMaginDistance = 20;
    private void addDots() {
        ImageView mOne_dot = new ImageView(getContext());
        mOne_dot.setImageResource(R.drawable.circle1_background_shape);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(mLeftRightMaginDistance, 0, mLeftRightMaginDistance, 0);

        in_ll.addView(mOne_dot, layoutParams);

        ImageView mTwo_dot = new ImageView(getContext());
        mTwo_dot.setImageResource(R.drawable.circle1_background_shape);
        in_ll.addView(mTwo_dot, layoutParams);

        ImageView mThree_dot = new ImageView(getContext());
        mThree_dot.setImageResource(R.drawable.circle1_background_shape);
        in_ll.addView(mThree_dot, layoutParams);
    }


    private void getDistance() {
        rl_dots.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //获得两个圆点之间的距离
                mDistance = in_ll.getChildAt(1).getLeft() - in_ll.getChildAt(0).getLeft();
                rl_dots.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                setLightPoint(0,0);
            }
        });
    }

    private void setLightPoint(int position, float positionOffset){
        float leftMargin = mDistance * (position + positionOffset);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mLight_dots.getLayoutParams();
        params.leftMargin = (int) leftMargin + mLeftRightMaginDistance;
        mLight_dots.setLayoutParams(params);
    }

    BRA_BS_Fragment mBRA_BS_Fragment;
    BRA_XL_Fragment mBRA_XL_Fragment;
    BRA_SHM_Fragment mBRA_SHM_Fragment;

    @Override
    protected void initData(Bundle arguments) {
        super.initData(arguments);

        setTimeStr(1);

        mBRA_BS_Fragment=new BRA_BS_Fragment();
        mBRA_XL_Fragment=new BRA_XL_Fragment();
        mBRA_SHM_Fragment=new BRA_SHM_Fragment();

        listFragment=new ArrayList<>();
        listFragment.add(mBRA_BS_Fragment);
        listFragment.add(mBRA_XL_Fragment);
        listFragment.add(mBRA_SHM_Fragment);

        mBraFragmentAdapter = new BraFragmentAdapter(getChildFragmentManager(),listFragment);
        mViewPager.setAdapter(mBraFragmentAdapter);
        mViewPager.setOffscreenPageLimit(3);
        pageSelectedPosition=0;
    }


    //1 显示 当前:1月15日
    //2 显示 当前:1月15日 09:45:45
    private String setTimeStr(int index){
        String timeStr = "";
        if (index==1){
            timeStr=getString_(R.string.dangqian)+":"+ SpecialText.formatDateStampString1(System.currentTimeMillis());
        }
        ViewUtils.setTextViewStr(time_tv,timeStr);

        return timeStr;
    }











    public void setSleepInfo(SleepInfo sleepInfo) {
        if (mBRA_SHM_Fragment!=null) mBRA_SHM_Fragment.setSleepInfo(sleepInfo);
    }

    public void setRealTimeData(WalkBean walkBean) {
        setTimeStr(1);
        if (mBRA_BS_Fragment!=null) mBRA_BS_Fragment.setRealTimeData(walkBean);
    }
    //设置实时心率
    public void setRealTimeXLData(RateBean rateBean) {
        setTimeStr(1);
        if (mBRA_XL_Fragment!=null)mBRA_XL_Fragment.setRealTimeXLData(rateBean);
    }


    public void SetBraTodayData(BraTodayData braTodayData) {
        if (mBRA_SHM_Fragment!=null) mBRA_SHM_Fragment.SetBraTodayData(braTodayData);
        if (mBRA_XL_Fragment!=null)mBRA_XL_Fragment.SetBraTodayData(braTodayData);
        if (mBRA_BS_Fragment!=null) mBRA_BS_Fragment.SetBraTodayData(braTodayData);

    }

//    public void stepOneDayAllInfo(StepOneDayAllInfo stepOneDayAllInfo) {
//        if (mBRA_BS_Fragment!=null) mBRA_BS_Fragment.stepOneDayAllInfo(stepOneDayAllInfo);
//    }
}
